<?php namespace Phpcmf\Controllers\Member;

/**
 * 二次开发时不可以修改本文件，影响升级覆盖
 */

class Shenqingdan extends \Phpcmf\Member\Mform
{

    public function index() {
        $this->_Member_List();
    }

    public function add() {
        //判断是post
        if((IS_POST)){
            $data = \Phpcmf\Service::L('input')->post('data');
            if(!$data){
                $this->_json('0', '数据格式错误');
            }
            // 费用检测
            $jine = (int)$data['jine'];
            $money = (int)$this->member['money'];
            if($jine > $money){
                $this->_json('0', '你的余额不足，请充值');
            }
            //调用模型
            $model = \Phpcmf\Service::M()->db->table('1_chuangyejin')->where('tieid', $data['tieid'])->where('quanid', $data['quanid'])->get()->getRowArray();
            //判断是否存在
            if(!$model){
                $this->_json('0', '帖子不存在');
            }
            //统计
            $count_uid = \Phpcmf\Service::M()->db->table('1_chuangyejin_form_shenqingdan')->where('tieid', $data['tieid'])->where('quanid', $data['quanid'])->where('shenqingren', $this->uid)->countAllResults();
            if($count_uid >= $model['sqsc']){
                //提示
                $tishi_uid = '已申请过'.$count_uid .'次了';
                $this->_json('0', $tishi_uid);
            }
            //总的超出提示
            $count_all = \Phpcmf\Service::M()->db->table('1_chuangyejin_form_shenqingdan')->where('tieid', $data['tieid'])->where('quanid', $data['quanid'])->countAllResults();
            if($count_all >= $model['shuliang']){
                //提示
                $tishi_all = '已经申请'.$count_all.'份，没有名额了';
                $this->_json('0', $tishi_all);
            }
            //
            $data_add = array(
               
                'uid'=> (int)$this->uid, //发布人的uid
                'catid'=> 1, //类型
                'cid' => $data['cid'],
                'author' => '',
                'inputip' => \Phpcmf\Service::L('input')->ip_info(),
                'inputtime' => time(),
                'title' => $data['title'],
                'status' => 1,
                'tableid' => 0, //附表id ，不用修改
                'displayorder' => 0,
                'zhuangtai' => 1,
                'chuangyejin' => $data['chuangyejin'],
                'jine' => $data['jine'],
                'quanid' => $data['quanid'],
                'tieid' => $data['tieid'],
                'shenqingren' => $this->uid,
               
               
               
            );
            //插入数据
            $insert_id = \Phpcmf\Service::M()->db->table('1_chuangyejin_form_shenqingdan')->insert($data_add);
            $id = \Phpcmf\Service::M()->db->insertID();
            $datac = [
                'id' => $id,
                'uid'=> (int)$this->uid, //发布人的uid
                'catid'=> 1, //类型
                'cid' => $data['cid'],
            ];
            \Phpcmf\Service::M()->db->table('1_chuangyejin_form_shenqingdan_data_0')->insert($datac);
            //给申请创业金用户添加创业金数据到个人账号
            $add_chuangyejin = (int)$data['chuangyejin'] + (int)$data['jine'];
            \Phpcmf\Service::M('member')->add_score($this->uid, $add_chuangyejin, $note = '《'.$data['title'].'》申请创业金', $url = '', $mark = '', $count = 0);
            //减去申请用户的人民币
            $reduce_jine = (int)$data['jine'];
            \Phpcmf\Service::M('member')->add_money($this->uid, -$reduce_jine);
            \Phpcmf\Service::M('Pay')->add_paylog([
                'uid' => $this->uid,
                'username' => $this->member['username'],
                'touid' => $model['uid'],
                'tousername' => '',
                'mid' => 'system', // 特征字符串，system表示系统
                'title' => '支出《'.$data['title'].'》申请创业金费用',
                'value' => -$data['jine'], // 交易金额，正数表示增加金额，负数表示消费金额
                'type' => 'finecms', // 支付方式，alipy，weixin，finecms表示余额
                'status' => 1, // 1表示交易成功,0表示未成功
                'result' => "申请创业金支付费用",
                'paytime' => SYS_TIME, // 付款时间，默认当前时间
                'inputtime' => SYS_TIME, // 流水参生时间，默认当前时间
            ]);
            //添加给圈主的创业金
             \Phpcmf\Service::M('member')->add_money($model['uid'], $reduce_jine);
             \Phpcmf\Service::M('Pay')->add_paylog([
                 'uid' => $model['uid'],
                 'username' => $this->member['username'],
                 'touid' => $this->uid,
                 'tousername' => '',
                 'mid' => 'system', // 特征字符串，system表示系统
                 'title' => '收入《'.$data['title'].'》申请创业金费用',
                 'value' => $data['jine'], // 交易金额，正数表示增加金额，负数表示消费金额
                 'type' => 'finecms', // 支付方式，alipy，weixin，finecms表示余额
                 'status' => 1, // 1表示交易成功,0表示未成功
                 'result' => "申请创业金收取费用",
                 'paytime' => SYS_TIME, // 付款时间，默认当前时间
                 'inputtime' => SYS_TIME, // 流水参生时间，默认当前时间
             ]);

            if($insert_id){
                $this->_json('1', '提交成功');
            } else {
                $this->_json('0', '提交失败');
            }
            $this->_json('1', '提交成功');
        } else {
            $this->_json('0', '无效访问');
        }
    }

    public function edit() {
        $this->_Member_Edit();
    }

    public function order_edit() {
        $this->_Member_Order();
    }

    public function del() {
        $this->_Member_Del();
    }
}
